layui.use(['table','form','jquery','backend'],function () {
    let table = layui.table;
    let form = layui.form;
    let $ = layui.jquery;
    let backend = layui.backend;

    backend.table.curd({
        id:"table",
        elem: '#table',
        url: '/admin/admin/index',
        cols: [
            [
                {type:'checkbox'},
                {title: '账号', field: 'username', align:'center', width:100},
                {title: '昵称', field: 'nickname', align:'center'},
                {title: '头像', field: 'avatar', align:'center', templet: "#avatar"},
                {title: '邮箱', field: 'email', align:'center'},
                {title: '登录失败次数', field: 'loginfailure', align:'center'},
                {title: '最后登录时间', field: 'logintime', align:'center'},
                {title: '状态', field: 'status', align:'center', templet:'#status'},
                {title: '操作', toolbar: '#bar', align:'center', width:195}
            ]
        ],
        toolbar: '#toolbar',
    });

    // 这里监听了表格事件 backend中的事件监听就失效了 就需要手动在绑定一次
    table.on('tool(table)', function (obj) {
        if (obj.event == 'sendmsg') {
            layer.prompt({
                formType: 0,
                value: '',
                title: '发送飞信',
            }, function(value, index, elem){
                layer.close(index);
                backend.api.ajax({
                    url : '/admin/admin/sendmsg',
                    data : {id : obj.data.id, msg: value}
                });
            });
        }
        var clickObj = $(this);
        var functionName = "event_tool_" + obj.event;
        backend.table.commonBindEvent(clickObj, obj, functionName);
    });

    form.on('submit(search)', function(data){
        table.reload('table',{
            where:{
                search : data.field
            }
        });
        return false;
    });

    $(".search-reset").on('click', function () {
        table.reload('table', {where:null});
    })
})
